package com.guiji.quartz.domain;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONNull;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import cn.wanghaomiao.xpath.model.JXDocument;
import com.guiji.quartz.config.ProxyConfig;
import com.guiji.quartz.task.DataSaveUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.net.*;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @program: cms-vue-plus
 * @description:
 * @author: wangxw
 * @date 2021/11/23 18:11
 */
public class Seekingalpha {
	private static final Logger log = LoggerFactory.getLogger(Seekingalpha.class);

	public static void mainMethod() throws Exception {
		HttpResponse response = null;
		try {
			response = HttpRequest.get("https://seekingalpha.com/api/v3/news?filter[category]=market-news%3A%3Aipos&filter[since]=0&filter[until]=0&include=author%2CprimaryTickers%2CsecondaryTickers&isMounting=true&page[size]=25&page[number]=1")
				.header("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9")
				.header("Accept-Encoding", "gzip, deflate")
				.setConnectionTimeout(30000)
				.execute();

		} catch (Exception e) {
			log.error("访问 https://seekingalpha.com/market-news/ipos 失败" + e);
			return;
		}
		if (null != response && response.getStatus() == 200) {
			String resp = response.body();
			JSONObject jobj = JSONUtil.parseObj(resp);
			JSONArray data = (JSONArray) jobj.get("data");
			if (null != data && data.size() > 0) {
				getContent(data);
			}

		} else {
			log.error("https://seekingalpha.com/market-news/ipos " + String.valueOf(response.getStatus()));
		}

	}

	private static void getContent(JSONArray data) throws InterruptedException {
		for (int k = 0; k < data.size(); k++) {
			Thread.sleep(1000);
			String url = null;
			String title = null;
			String img = null;
			String content = null;
			Set<String> newImgs = new HashSet<>();
			try {
				JSONObject jobj = (JSONObject) data.get(k);
				JSONObject attributes = (JSONObject) jobj.get("attributes");
				title = (String) attributes.get("title");
				content = (String) attributes.get("content");
				if (!(attributes.get("gettyImage") instanceof JSONNull)) {
					JSONObject gettyImage = (JSONObject) attributes.get("gettyImage");
					if (null != gettyImage) {
						img = (String) gettyImage.get("medium");
					} else {
						if (!(attributes.get("gettyImageUrl") instanceof JSONNull)) {
							img = (String) attributes.get("gettyImageUrl");
							if (null == img) {
								continue;
							}
						}
					}
				}
				JSONObject links = (JSONObject) jobj.get("links");
				String canonical = (String) links.get("canonical");
				url = canonical;
				newImgs.add(img);
				content = content.replaceAll("</p>.*?\n", "\\$\\$\\$").replaceAll("<figcaption.*?</figcaption>", "")
					.replaceAll("</ul>.*?\n", "\\$\\$\\$")
					.replaceAll("</li>.*?\n", "\\$\\$\\$")
					.replaceAll("\n", "").replaceAll("<div id=.*?</div>", " ")
					.replaceAll("<(.|\n)*?>", " ");
				if (null != content && content.length() > 50000 || content.replaceAll("\\$|[\\s\\p{Zs}]", "").length() < 20 || newImgs.size() > 20) {
					continue;
				}
				Date date = new Date();
				DataSaveUtil.saveData(title, content, newImgs, url, date, "https://seekingalpha.com/market-news/ipos", "Seekingalpha-IPONews");
				log.info(Thread.currentThread().getName() + "-----------------Seekingalpha-IPONews----------------");
			} catch (Exception e) {
				System.out.println(e);
				continue;
			}

		}
	}


//	public static void main(String[] args) throws Exception {
//		mainMethod();
//	}

}



